package com.oracle.calligraphy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.calligraphy.dao.UserDao;
import com.oracle.calligraphy.entity.UserEntity;
import com.oracle.calligraphy.service.UserService;
import com.oracle.common.utils.Compents;
import com.oracle.common.utils.PageUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public PageUtils queryPage(Integer pageNum,Integer pageSize,Map<String, Object> params) {
        //判断分页数据
        Integer currentNum = Compents.PAGENUM;
        Integer pSize = Compents.PAGESIZE;

        //如果存在页码
        if(!ObjectUtils.isEmpty(pageNum) ){
            currentNum = pageNum;
        }
        if(!ObjectUtils.isEmpty(pageSize) ){
            pSize = pageSize;
        }

        //查询前加分页数据
        PageHelper.startPage(pageNum,pageSize);
        //执行查询 将查询回来的结果 封装到 Page
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("username",params.get("username"));

        Page<UserEntity> page = (Page<UserEntity>)userDao.selectList(wrapper);

        return new PageUtils(page.getTotal(),page.getResult());
    }

}
